|
|
|
|
|
|
|
Select Case UCase(txtOperation.Text)
Case "WASH"
' Do Something
Case "RINSE"
' Do Something Else
Case "SPIN"
' Do Something Else Yet
Case Else
MsgBox "Invalid Entry!"
End Select |
|
|
|
|
|
|
|
|
In the preceding code, if the UCase function had not been used, the user would receive the Invalid Entry message even if he or she had entered a "correct" choice in lowercase or mixed case (Rinse, for example). |
|
|
|
|
| |
|
|
|
|
A common practice is to nest string functions within statements rather than use an extra variable to store each intermediate step. For example, consider the following Select Case statement, which uses a bunch of nested string functions to help with input validation. Users can come up with any capitalization they want to, but it will be handled by the code. |
|
|
|
|
|
|
|
|
stUserInput = InputBox$("Type Yes or No, please!")
Select Case Left$(Trim$(UCase$(stUserInput)),1)
Case "Y"
MsgBox "Yes"
Case "N"
MsgBox "No"
Case Else
MsgBox "type YES or NO please!"
End Select |
|
|
|
|
|
|
|
|
|
|
Another Visual Basic function, StrConv, performs special conversions of strings. Most of the conversions it can perform are either redundant (converting to all uppercase or all lowercase, for example) or beyond the scope of this book (converting between different types of Japanese characters), but one of its conversion types is worth mentioning here. StrConv can convert a string to proper case, in which the first letter of each word is capitalized. The following code sample demonstrates this technique: |
|
|
|
|
|
|
|
|
lblHeadline = StrConv(stHeadline, vbProperCase) |
|
|
|
|
|
|
|
|
Examples of using UCase and LCase, as well as StrConv, are illustrated in Figure 9.4. |
|
|
|
|
|
|
|
|
Figure 9.4
You can use LCase, UCase, and StrConv to
modify the case of the letters in a string of text. |
|
|
|
|
|